package com.example;

import java.io.File;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

public class TomcatStarter {
    
    public static void main(String[] args) throws LifecycleException {
        Tomcat tomcat = new Tomcat();
        tomcat.getConnector();
        // 设置webapp目录, 获得webapp的绝对路径并以此创建上下文
        Context context =  tomcat.addWebapp("", new File("module-tomcat/src/main/webapp").getAbsolutePath());
        

        // 对接servlet
        WebResourceRoot resources = new StandardRoot(context);
        // DirResourceSet 相当于webapp中的一个项目文件夹
        resources.addPreResources(
                new DirResourceSet(resources, "/WEB-INF/classes",
                        new File("module-servlet/target/classes").getAbsolutePath(), "/"));     // target/classes 为存放servlet类的目录
        context.setResources(resources);
        
        
        
        tomcat.start();
        System.out.println("Tomcat 服务器已启动... 打开:http://localhost:8080/hello");
        tomcat.getServer().await();
    }
}
